package com.gymmsystem.GYMMSystem.service.impl;

import com.gymmsystem.GYMMSystem.entity.UserMessage;
import com.gymmsystem.GYMMSystem.mapper.UserMessageMapper;
import com.gymmsystem.GYMMSystem.service.UserMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UserMessageServiceImpl implements UserMessageService {

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Override
    public List<UserMessage> findMessagesByUserId(Long userId) {
        return userMessageMapper.findMessagesByUserId(userId);
    }

    @Override
    public int countUnreadMessages(Long userId) {
        return userMessageMapper.countUnreadMessages(userId);
    }

    @Override
    public boolean markAsRead(Long id) {
        if (id == null) {
            return false;
        }

        // 校验消息是否存在
        if (userMessageMapper.selectById(id) == null) {
            return false;
        }

        int result = userMessageMapper.markAsRead(id);
        return result > 0;
    }

    @Override
    public boolean markAllAsRead(Long userId) {
        if (userId == null) {
            return false;
        }

        int result = userMessageMapper.markAllAsRead(userId);
        return result > 0;
    }

    @Override
    public boolean insertBookingMessage(Long userId, String content, Long reservationId) {
        if (userId == null || content == null || content.trim().isEmpty() || reservationId == null) {
            return false;
        }

        int result = userMessageMapper.insertBookingMessage(userId, content, reservationId);
        return result > 0;
    }

    @Override
    public boolean insertCancelMessage(Long userId, String content, Long reservationId) {
        if (userId == null || content == null || content.trim().isEmpty() || reservationId == null) {
            return false;
        }

        int result = userMessageMapper.insertCancelMessage(userId, content, reservationId);
        return result > 0;
    }

    @Override
    public List<UserMessage> findMessagesByReservationId(Long reservationId) {
        if (reservationId == null) {
            return null;
        }

        return userMessageMapper.findMessagesByReservationId(reservationId);
    }

    @Override
    public boolean deleteMessagesByUserId(Long userId) {
        if (userId == null) {
            return false;
        }

        int result = userMessageMapper.deleteMessagesByUserId(userId);
        return result > 0;
    }

    @Override
    public boolean deleteReadMessages(Long userId) {
        if (userId == null) {
            return false;
        }

        int result = userMessageMapper.deleteReadMessages(userId);
        return result > 0;
    }

    @Override
    public UserMessage getMessageById(Long id) {
        if (id == null) {
            return null;
        }
        return userMessageMapper.selectById(id);
    }

    @Override
    public Long createMessage(UserMessage userMessage) {
        if (userMessage == null ||
                userMessage.getUserId() == null ||
                userMessage.getTitle() == null || userMessage.getTitle().trim().isEmpty() ||
                userMessage.getContent() == null || userMessage.getContent().trim().isEmpty()) {
            return null;
        }

        // 设置默认值
        if (userMessage.getIsRead() == null) {
            userMessage.setIsRead(0); // 默认未读
        }
        if (userMessage.getType() == null) {
            userMessage.setType("SYSTEM_NOTICE"); // 默认系统通知
        }
        if (userMessage.getCreatedAt() == null) {
            userMessage.setCreatedAt(LocalDateTime.now());
        }

        int result = userMessageMapper.insert(userMessage);
        return result > 0 ? userMessage.getId() : null;
    }
}